﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StructureMap;
using Lucid.ESA.ClientDataSystem.Application.Contracts;
using Lucid.ESA.ClientDataSystem.Application.Responses;
using Lucid.ESA.ClientDataSystem.Application.DataTransferObjects;
using Lucid.ESA.ClientDataSystem.Application.Requests;

namespace Lucid.ESA.ClientDataSystem.UI.Controllers
{
    public class ContactController : Controller
    {
        public ActionResult Index(long clientId)
        {
            IClientService clientService = ObjectFactory.GetInstance<IClientService>();
            GetResponse<ClientDto> response = clientService.GetClientById(clientId) as GetResponse<ClientDto>;
            ViewData.Add("clientId", clientId);
            return View(response.Value.Contacts);
        }

        public ActionResult Details(long id)
        {
            IContactService contactService = ObjectFactory.GetInstance<IContactService>();
            GetResponse<ContactDto> response = contactService.GetContactById(id) as GetResponse<ContactDto>;
            return View(response.Value);
        }

        public ActionResult Edit(long id)
        {
            IContactService contactService = ObjectFactory.GetInstance<IContactService>();
            ContactDto contactDto = (contactService.GetContactById(id) as GetResponse<ContactDto>).Value;

            EditContactRequest editContactRequest = new EditContactRequest
            {
                ContactId = contactDto.Id.Value,
                Title = contactDto.Title,
                Forename = contactDto.Forename,
                Surname = contactDto.Surname,
                EmailAddress = contactDto.EmailAddress,
                TelephoneNumber = contactDto.TelephoneNumber
            };

            ViewData.Add("clientId", contactDto.Client.Id.Value);
            return View(editContactRequest);
        }

        [HttpPost]
        public ActionResult Edit(EditContactRequest request, long clientId)
        {
            IContactService contactService = ObjectFactory.GetInstance<IContactService>();
            Response response = contactService.EditContact(request);

            if (response.Status == ResponseStatus.Success)
            {
                return RedirectToAction("/Index", new { clientId = clientId });
            }
            else
            {
                ValidationErrorResponse validationErrorResponse = response as ValidationErrorResponse;
                foreach (ValidationErrorResponseMessage error in validationErrorResponse.Errors)
                    ViewData.ModelState.AddModelError(error.Field == null ? "" : error.Field, error.Text);

                return Edit(request.ContactId);
            }
        }

        public ActionResult Create(long clientId)
        {
            ViewData.Add("clientId", clientId);
            return View();
        }

        [HttpPost]
        public ActionResult Create(CreateContactRequest request)
        {
            IContactService contactService = ObjectFactory.GetInstance<IContactService>();
            Response response = contactService.CreateContact(request);

            if (response.Status == ResponseStatus.Success)
            {
                return RedirectToAction("/Index", new { clientId = request.ClientId });
            }
            else
            {
                ValidationErrorResponse validationErrorResponse = response as ValidationErrorResponse;
                foreach (ValidationErrorResponseMessage error in validationErrorResponse.Errors)
                    ViewData.ModelState.AddModelError(error.Field == null ? "" : error.Field, error.Text);

                return Create(request.ClientId);
            }
        }
    }
}
